package com.gtw.modules.api.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.AuthIgnoreApi;
import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.dto.TdAddressDto;
import com.gtw.modules.api.service.TbAddressService;
import com.gtw.modules.base.service.TbCityService;
import com.gtw.modules.base.service.TbDistrictService;
import com.gtw.modules.base.service.TbProvinecService;
import com.gtw.modules.dao.TbAddressDao;
import com.gtw.modules.entity.TbAddress;
import com.gtw.modules.entity.TbCollection;
import com.gtw.modules.entity.TbUser;
import com.gtw.utils.EntityDtoConverter;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 地址表
 *
 * @author wuye
 * @email www.baidu.com
 * @date 2018-02-06 11:38:12
 */
@RestController
@RequestMapping("/api/address")
public class TbAddressController {
    @Autowired
    private TbAddressService tbAddressService;
    @Autowired
    private TbAddressDao tbAddressDao;
    @Autowired
    private TbCityService tbCityService;
    @Autowired
    private TbDistrictService tbDistrictService;
    @Autowired
    private TbProvinecService tbProvinecService;

    /**
     * 查询列表
     *
     * @returnapi
     */
    @RequestMapping("/list")
    public R list(@LoginUserApi TbUser tbUser) {
        //查询列表数据
        List<TbAddress> tbAddressList = tbAddressService.findbyUid(tbUser.getUuid());
        EntityDtoConverter edc = new EntityDtoConverter();
        List<TdAddressDto> tdAddressDtoList = new ArrayList<>();
        for (int i = 0; i < tbAddressList.size(); i++) {
            TdAddressDto tdAddressDto = new TdAddressDto();
            try {
                tdAddressDto = (TdAddressDto) edc.entityConvertDto(tbAddressList.get(i), tdAddressDto);
                tdAddressDto.setCityname(tbCityService.findByCityId(tbAddressList.get(i).getCityid()).getCityname());
                tdAddressDto.setDistrictname(tbDistrictService.findByDistrictId(tbAddressList.get(i).getDistrictid()).getDistrictname());
                tdAddressDto.setProvincename(tbProvinecService.queryObject(tbAddressList.get(i).getProvinceid()).getProvincename());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            tdAddressDtoList.add(tdAddressDto);
        }
        return R.ok().put("data", tdAddressDtoList);
    }


    /**
     * 信息
     */
    @AuthIgnoreApi
    @RequestMapping("/info")
    public R info(Integer id) {
        EntityDtoConverter edc = new EntityDtoConverter();
        TbAddress address = tbAddressService.queryObject(id);
        TdAddressDto tdAddressDto = new TdAddressDto();
        try {
            tdAddressDto = (TdAddressDto) edc.entityConvertDto(address, tdAddressDto);
            tdAddressDto.setCityname(tbCityService.findByCityId(address.getCityid()).getCityname());
            tdAddressDto.setDistrictname(tbDistrictService.findByDistrictId(address.getDistrictid()).getDistrictname());
            tdAddressDto.setProvincename(tbProvinecService.queryObject(address.getProvinceid()).getProvincename());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return R.ok().put("data", tdAddressDto);
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    public R save(@RequestBody TbAddress address, @LoginUserApi TbUser tbUser) {
        if (address.getId() != null) {
            tbAddressService.update(address);
        } else {
            address.setUid(tbUser.getUuid());
            tbAddressService.save(address);
        }


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbAddress address, @LoginUserApi TbUser tbUser) {
        tbAddressService.update(address);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public R delete(@RequestBody Integer[] ids, @LoginUserApi TbUser tbUser) {
        for (int i = 0; i < ids.length; i++) {
            tbAddressService.delete(ids[i]);
        }
        return R.ok();
    }

    /**
     * 查询省市区地址列表
     */
    @AuthIgnoreApi
    @RequestMapping("/pub")
    public R pub(int provinceid, int cityid) {
        //如果省份与城市ID 均为空 则查询全部省份
        if (provinceid == 0 && cityid == 0) {
            return R.ok().put("data", tbProvinecService.findAll());
        }
        //如果城市ID不为空，则查询该城市下的所有区域
        if (cityid != 0) {
            return R.ok().put("data", tbDistrictService.findByCityId(cityid));
        }
        //如果省份ID不为空，则查询该省份下的所有城市
        if (provinceid != 0) {
            return R.ok().put("data", tbCityService.findbyProvinecId(provinceid));
        }
        return R.ok();
    }

}
